package net.hancl.modules.sys.controller;

import net.hancl.common.bean.ResultBean;
import net.hancl.modules.sys.bean.UserBean;
import net.hancl.modules.sys.entity.Menu;
import net.hancl.modules.sys.service.IMenuService;
import net.hancl.modules.sys.service.IUserService;
import net.hancl.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping(value = "sys/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IUserService userService;

    @Autowired
    private UserUtils userUtils;

    @RequestMapping(value = "getMenu", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean getMenu(@RequestParam("id") String id){
        ResultBean rb = new ResultBean();
        Menu menu = menuService.get(id);
        if(menu != null){
            rb.setMessageCode("001");
            rb.setData(menu);
        }else{
            rb.setMessageCode("002");
        }
        return rb;
    }

    @RequestMapping(value = "op",method = RequestMethod.POST)
    @ResponseBody
    public ResultBean op(Menu menu, @RequestParam("op") String op){
        System.out.println(menu.getParentId());
        ResultBean rb = new ResultBean();
        int count = menuService.op(menu, op);
        if(count>0){
            rb.setMessageCode("001");
        }else{
            rb.setMessageCode("002");
        }
        return rb;
    }

    @RequestMapping(value = "updateMenu", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean updateMenu(Menu menu){
        ResultBean rb = new ResultBean();
        int count = menuService.update(menu);
        if(count>0){
            rb.setMessageCode("001");
        }else{
            rb.setMessageCode("002");
        }
        return rb;
    }

    @RequestMapping(value = "deleteMenu", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean deleteMenu(String id){
        ResultBean rb = new ResultBean();
        int count = menuService.del(id);
        if(count>0){
            rb.setMessageCode("001");
        }else{
            rb.setMessageCode("002");
        }
        return rb;
    }

    //todo---------------
    @RequestMapping(value = "queryAllMenuRel", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean queryAllMenuRel(){
        ResultBean rb = new ResultBean();
        UserBean user = userUtils.getUserBean();
        List<Menu> menus = menuService.queryAllMenuByCurrentName(user, true);
        rb.setMessageCode("001");
        rb.setData(menus);
        return rb;
    }

    @RequestMapping(value = "queryAllMenu", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean queryAllMenu(){
        ResultBean rb = new ResultBean();
        List<Menu> menus = menuService.getMenuListAll();
        rb.setMessageCode("001");
        rb.setData(menus);
        return rb;
    }
}
